package com.defangit.applicant.orm;

import com.defangit.applicant.domain.Enroll;
import com.defangit.applicant.domain.TicketType;
import laogen.hibernate.CommonFinder;
import laogen.hibernate.CommonService;
import laogen.lang.exception.ParameterNotValidException;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import java.util.List;

public class TicketTypeService extends CommonService<TicketType>{
//    public TicketType addTicketType(TicketType ticketType){
//        if (ticketType==null)throw ParameterNotValidException.emptyOrNull("添加的票种");
//        if (ticketType.getTicketName()==null) throw ParameterNotValidException.nullParameter("要添加的票种名");
//        add(ticketType);
//        commitSession();
//        return ticketType;
//    }
//
//    public TicketType updateTicketType(TicketType ticketType){
//        TicketType tt=get(ticketType.getTicketTypeId())
//                .orElseThrow(()->ParameterNotValidException.zeroIdOrEntityNonExist("要修改的票种"));
//        update(ticketType);
//        return ticketType;
//    }
    public boolean saveOrUpdateTicketType(int enrollId,List<TicketType> ticketType){
        Enroll enroll = getSession().get(Enroll.class,enrollId);
        if (enroll==null) throw ParameterNotValidException.zeroIdOrEntityNonExist("报名活动id");
        for (TicketType ticketTypes :ticketType){
            ticketTypes.setEnroll(enroll);
            if (ticketTypes.getMaxLength()==0||ticketTypes.getTicketName()==null)
                return false;
            //初始化时如果不存在可用数量,则可用数量为最大值
            if (!(ticketTypes.getAllowedTickets()>0))
                ticketTypes.setAllowedTickets(ticketTypes.getMaxLength());

        }
        saveOrUpdate(ticketType);
        commitSession();
        return true;
    }

    public boolean delTicketType(Integer tid){
        TicketType tt=get(tid).orElseThrow(()->ParameterNotValidException.zeroIdOrEntityNonExist("要删除的票种"));
        tt.getEnroll().getTicketTypes().remove(tt);
        tt.setEnroll(null);
        delete(tt);
        return true;
    }

    public TicketTypeFinder findAll(){
        return new TicketTypeFinder();
    }
    public class TicketTypeFinder extends CommonFinder<TicketType>{
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(TicketType.class);
        @Override
        public Criteria getCriteria() {
            return detachedCriteria.getExecutableCriteria(getSession());
        }

        @Override
        public DetachedCriteria getDetachedCriteria() {
            return detachedCriteria;
        }

        public TicketTypeFinder ttName(String ttname){
            if (ttname!=null)
            detachedCriteria.add(Restrictions.like("ticketName",ttname, MatchMode.ANYWHERE));
            return this;
        }
        public TicketTypeFinder allowedTikcet(Integer allowedTikcet){
            if (allowedTikcet!=null&&allowedTikcet!=0)
                detachedCriteria.add(Restrictions.gt("allowedTickets",allowedTikcet));
            return this;
        }
    }
}
